/* $Id:action_level.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "action_level"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "action_level.h"
#include "action.h"
#include "../general/general.h"

action_level::action_level() {
    nr=0;
    action=0;
    level=0;
}

// Field : nr

const int action_level::getNr() const { return nr; }
void action_level::setNr(const int v) { nr=v; }
int action_level::operator<(const class action_level &ck) const { return (this->nr < ck.nr)?1:0; }

// Field : action

class action& action_level::getAction() const { if (action==0) throw nullpointer(); return *action; }
void action_level::setAction(class action &v) { action=&v; }

// Field : level

const int action_level::getLevel() const { return level; }
void action_level::setLevel(const int v) { level=v; }

void action_level::save(class channel &ch) {
    ch.write("action_level", "");
    ch.write("n", getNr());
    ch.write("a", getAction().getName());
    ch.write("l", getLevel());
}

void action_level::load(class channel &ch) {
    if (ch.key=="n" || ch.key=="nr") { setNr(atoi(ch.value.c_str())); ch.next(); }
    if (ch.key=="a" || ch.key=="action") {
        if (!action_repo.isAction(ch.value)) {
            dbg_state("link unknown action %s", ch.value.c_str());
            class action rec;
            rec.setName(ch.value);
            action_repo.setAction(rec);
        }
        setAction(action_repo.getAction(ch.value));
        ch.next();
    }
    if (ch.key=="l" || ch.key=="level") { setLevel(atoi(ch.value.c_str())); ch.next(); }
}

void action_level::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<action_level nr=\"" << getNr() <<
    "\" level=\"" << getLevel() <<
    "\">" << endl;
    if (r<2) try {
        getAction().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    f << ncpy(l, "  ") << "</action_level>" << endl;
}
